﻿/* 13. *** Write a program that calculates for given N how many trailing zeros present at the end of the number N!. 
 * Examples: N = 10 --> N! = 3628800 --> 2; 	N = 20 --> N! = 2432902008176640000 --> 4. 
 * Does your program work for N = 50 000? 
 * Hint: The trailing zeros in N! are equal to the number of its prime divisors of value 5. Think why! */

using System;

public class TrailingZerosInFactorial
{
    public static void Main()
    {
        int n;
        int trailingZeros;
        int next;

        do
        {
            do
            {
                Console.Clear();
                Console.Write("N = ");
                if ((int.TryParse(Console.ReadLine(), out n)) && (0 <= n))
                    break;
            } while (true);

            Console.Write("The trailing zeros in {0}! are ", n);
            trailingZeros = 0;

            if (n == 0)
                trailingZeros = 1;
            else
            {
                for (int i = 1; i <= n; i++)
                {
                    next = i;
                    do
                    {
                        if (next % 5 == 0)
                        {
                            trailingZeros++;
                            next /= 5;
                        }
                        else
                            break;
                    } while (true);
                }
            }

            Console.WriteLine("{0}.", trailingZeros);
            Console.ReadKey();
        } while (true);
    }
}